Imports System.Text.RegularExpressions
Imports System.Text

Public Class FrmMain    
    Private KeywordList As ArrayList = New ArrayList

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        If Regex.Match(LCase(Me.txtUrl.Text), "http://").Value = String.Empty Then
            Me.txtUrl.Text = "http://" & Me.txtUrl.Text
        End If
        If Me.txtUrl.Text <> String.Empty Then
            Cursor = Cursors.WaitCursor
            FindUrl(Me.txtUrl.Text)
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub Clear()
        Me.txtHtml.Text = String.Empty
        Me.txtResult.Text = String.Empty
        Me.btnCount.Enabled = False
        Me.txtKeyword.Text = String.Empty
        Me.txtKeyword.Enabled = False
        Me.txtUrl.Text = String.Empty
        Me.txtUrl.Focus()
    End Sub

    Private Sub FindUrl(ByVal url As String)
        Try
            Me.Clear()
            Me.txtUrl.Text = url
            Dim objNet As Net.WebClient = New Net.WebClient
            Dim objUTF As UTF8Encoding = New UTF8Encoding
            Dim objHtml As String = String.Empty

            objHtml = objUTF.GetString(objNet.DownloadData(url))
            txtHtml.Text = objHtml

            If Me.txtHtml.Text.Length > 0 Then
                Me.btnCount.Enabled = True
                Me.txtKeyword.Enabled = True
            End If

            Me.SetWebsite(url)
            Me.FindTitle(objHtml)
            Me.FindMeta(objHtml)
            Me.FindKeyword(objHtml)
            Me.FindTotalKeyword(objHtml)
        Catch ex As Exception
            MsgBox("Cannot find url", MsgBoxStyle.Information, My.Resources.AppTitle)
        End Try
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.Clear()
    End Sub

    Public Sub SetWebsite(ByVal url As String)
        Me.txtResult.Text = url.ToString.Substring(7) & vbLf
        Me.Spliter()
    End Sub

    Private Sub FindTitle(ByVal web As String)
        Try
            Dim objResult As Match

            'Set Title
            Me.txtResult.Text = Me.txtResult.Text & My.Resources.Title & vbLf
            Me.Spliter()

            objResult = Regex.Match(web, "<[Tt][Ii][Tt][Ll][Ee]>[a-zA-Z0-9-\x22\s\t\r//;=:,@#$%.~`&^!*_+()|\\{}\[\]'\?]*</[Tt][Ii][Tt][Ll][Ee]>")
            Me.txtResult.Text = Me.txtResult.Text & objResult.Value & vbLf & vbLf
            Me.Spliter()
        Catch ex As Exception
            MsgBox("Title not find", MsgBoxStyle.Information, My.Resources.AppTitle)
        End Try
    End Sub

    Private Sub FindMeta(ByVal web As String)
        Try
            Dim objResult As MatchCollection
            Dim x As Integer = 0

            objResult = Regex.Matches(web, "<[Mm][Ee][Tt][Aa][a-zA-Z0-9-\x22\s\t\r//;=:,@#$%.~`&^!*_+()|\\{}\[\]'\?]*</[Mm][Ee][Tt][Aa]>")

            If objResult.Count = 0 Then
                objResult = Regex.Matches(web, "<[Mm][Ee][Tt][Aa][a-zA-Z0-9-\x22\s\t\r//;=:,@#$%.~`&^!*_+()|\\{}\[\]'\?]*>")
            End If

            'Set title Meta List
            Me.txtResult.Text = Me.txtResult.Text & My.Resources.MetaLists & vbLf
            Me.Spliter()
            For x = 0 To objResult.Count - 1
                Me.txtResult.Text = Me.txtResult.Text & objResult.Item(x).Value & vbLf
            Next
            Me.txtResult.Text = Me.txtResult.Text & vbLf
            Me.Spliter()
        Catch ex As Exception
            MsgBox("Meta not find", MsgBoxStyle.Information, My.Resources.AppTitle)
        End Try
    End Sub

    Private Sub FindKeyword(ByVal web As String)
        Try
            Dim objComma As MatchCollection
            Dim objResult As Match
            Dim objResultCollection As MatchCollection
            Dim objTemp As String
            Dim x As Integer = 0

            Me.KeywordList.Clear()

            objResult = Regex.Match(web, "<[Mm][Ee][Tt][Aa][a-zA-Z0-9-\x22\s\t\r//;=:,@#$%.~`&^!*_+()|\\{}\[\]'\?]*[Kk][Ee][Yy][Ww][Oo][Rr][Dd][Ss][a-zA-Z0-9-\x22\s\t\r//;=:,@#$%.~`&^!*_+()|\\{}\[\]'\?]*</[Mm][Ee][Tt][Aa]>")

            If objResult.Value = String.Empty Then
                objResult = Regex.Match(web, "<[Mm][Ee][Tt][Aa][a-zA-Z0-9-\x22\s\t\r//;=:,@#$%.~`&^!*_+()|\\{}\[\]'\?]*[Kk][Ee][Yy][Ww][Oo][Rr][Dd][Ss][a-zA-Z0-9-\x22\s\t\r//;=:,@#$%.~`&^!*_+()|\\{}\[\]'\?]*>")
            End If

            objResult = Regex.Match(objResult.Value, "[Cc][Oo][Nn][Tt][Ee][Nn][Tt][a-zA-Z0-9-\x22\s\t\r//;=:,@#$%.~`&^!*_+()|\\{}\[\]'\?]*\x22")
            objTemp = objResult.Value

            objTemp = objTemp.Substring(8)
            objTemp = objTemp.Replace(".", "")
            objTemp = objTemp.Replace(Chr(34), "")

            'Find Comma
            objComma = Regex.Matches(objResult.Value, ",")
            If objComma.Count = 0 Then
                'Find One keyword 
                objResult = Regex.Match(objResult.Value, "[\w" & Chr(32) & "]+" & Chr(34))
                If objResult.Value <> String.Empty Then
                    Me.KeywordList.Add(objResult.Value.Replace(Chr(34), "").Trim)
                End If
            Else
                'Find keyword
                objResultCollection = Regex.Matches(objTemp, "[\w" & Chr(32) & "]+,")
                For x = 0 To objResultCollection.Count - 1
                    Me.KeywordList.Add(Trim(objResultCollection.Item(x).Value.Replace(",", "")))
                Next

                'Find last keyword 
                objResult = Regex.Match(objResult.Value, ",[\w" & Chr(32) & "]+" & Chr(34))
                If objResult.Value <> String.Empty Then
                    Me.KeywordList.Add(objResult.Value.Replace(",", "").Replace(Chr(34), "").Trim)
                End If
            End If

            'Set title Keyword List
            Me.txtResult.Text = Me.txtResult.Text & My.Resources.KeywordLists & vbLf
            Me.Spliter()
            For x = 0 To Me.KeywordList.Count - 1
                Me.txtResult.Text = Me.txtResult.Text & Me.KeywordList(x).ToString & vbLf
            Next
            Me.txtResult.Text = Me.txtResult.Text & vbLf
            Me.Spliter()
        Catch ex As Exception
            MsgBox("Keyword not find", MsgBoxStyle.Information, My.Resources.AppTitle)
        End Try
    End Sub

    Private Sub FindTotalKeyword(ByVal web As String)
        Try
            Dim objResult As MatchCollection
            Dim x As Integer = 0
            Dim z As Integer = 0

            'Set title Total Keyword
            Me.txtResult.Text = Me.txtResult.Text & My.Resources.TotalKeyword & vbLf
            Me.Spliter()
            For x = 0 To Me.KeywordList.Count - 1
                objResult = Regex.Matches(web, Me.KeywordList.Item(x).ToString)
                Me.txtResult.Text = Me.txtResult.Text & Me.KeywordList.Item(x).ToString & " : " & objResult.Count & vbLf
            Next
            Me.txtResult.Text = Me.txtResult.Text & vbLf
            Me.Spliter()
        Catch ex As Exception
            MsgBox("Keyword is not find", MsgBoxStyle.Information, My.Resources.AppTitle)
        End Try
    End Sub

    Private Sub Spliter()
        Dim Spliter As String = "--------------------------------------------------------------" & vbLf
        Me.txtResult.Text = Me.txtResult.Text + Spliter
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = My.Resources.AppTitle
    End Sub

    Private Sub txtUrl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUrl.KeyDown
        If e.KeyData = Keys.Enter Then
            btnFind_Click(sender, e)
        End If
    End Sub

    Private Sub mnuMuktisoftWebsite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMuktisoftWebsite.Click
        Try
            Dim objBrowser As WebBrowser = New WebBrowser
            objBrowser.Navigate("http://www.muktisoft.com", True)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, My.Resources.AppTitle)
        End Try
    End Sub

    Private Sub mnuAboutSoftware_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAboutSoftware.Click
        Try
            Dim objSplash As Splash = New Splash

            objSplash.ShowDialog()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, My.Resources.AppTitle)
        End Try
    End Sub

    Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        End
    End Sub

    Private Sub btnCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCount.Click
        Try
            Dim objResult As MatchCollection

            objResult = Regex.Matches(Me.txtHtml.Text, Me.txtKeyword.Text)

            If objResult.Count = 0 Then
                MsgBox("Word cannot find")
            ElseIf objResult.Count = 1 Then
                MsgBox("Total Word is used in page = " & objResult.Count & " time")
            ElseIf objResult.Count > 1 Then
                MsgBox("Total Words is used in page = " & objResult.Count & " times")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, My.Resources.AppTitle)
        End Try
    End Sub
End Class
